const Controller = require("egg").Controller;
const { success, fail, handle_status } = require("../utils/code");

class RoleController extends Controller {
  async add() {
    const { ctx } = this;
    const { role_name } = ctx.request.body;
    const { type, message } = await ctx.service.role.add(role_name);
    ctx.body = handle_status[type]({}, message);
  }

  async edit() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.role.edit(body);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  async list() {
    const { ctx } = this;
    const query = ctx.request.query;
    const result = await ctx.service.role.list(query);
    if (result) {
      ctx.body = success(result);
    } else {
      ctx.body = fail();
    }
  }

  async delete() {
    const { ctx } = this;
    const { id } = ctx.request.query;
    const result = await ctx.service.role.delete(id);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  // 角色分配菜单
  async add_menu() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.role.add_menu(body);
    
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }
}
module.exports = RoleController;
